﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainList
{
    public partial class uc_Locomotive_MainBase_TrainList_AddNewNum_Simple : DevExpress.XtraEditors.XtraForm
    {
        public string _TTID = "";
        public string _TTName = "";
        public string _TCID = "";
        public string _TCName = "";

        public uc_Locomotive_MainBase_TrainList_AddNewNum_Simple()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_TrainList_AddNewNum_Simple_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(cb_RB, "select f_id,f_name from TS_S_T_P_RailwayBureau where f_del=0");
            if (cb_RB.Items.Count > 0)
            {
                cb_RB.SelectedIndex = 0;
            }
            LoadTrainList();
        }
        private void LoadTrainList()
        {
            string strSQL = "select f_name,(select f_name from TS_S_T_P_RailwayBureau where f_id=TS_S_T_P_Train_List.f_arbid)+'-'+(select f_name from TS_S_T_P_RailwayStations where f_id=TS_S_T_P_Train_List.f_arsid) as f_rbname from TS_S_T_P_Train_List where f_ttid=" + _TTID + " and f_del=0 order by f_name asc";
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL,_dt);
            gc_Info.DataSource = _dt;
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            if (tb_Name.Text.Trim().Length != 4)
            {
                MessageBox.Show("车号必须是四位数字！");
            }
            else
            {
                if (TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_T_P_Train_List where f_del=0 and f_name='"+tb_Name.Text.Trim()+"' and f_ttid="+_TTID +" and f_del=0", "f_count") == "0")
                {
                    TS_Win_Class.MySqlCom("insert into TS_S_T_P_Train_List(f_name,f_ttid,f_ttname,f_arbid,f_arsid,f_del) values('" + tb_Name.Text.Trim() + "'," + _TTID + ",'" + _TTName + "'," + cb_RB.SelectedValue.ToString() + "," + cb_RS.SelectedValue.ToString() + ",0)");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("该机车号已存在！");
                }
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void cb_RB_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cb_RB.SelectedIndex > -1)
                {
                    TS_Win_Class.FillListTypeAct(cb_RS, "select f_id,f_name from TS_S_T_P_RailwayStations where f_del=0 and f_upid=" + cb_RB.SelectedValue.ToString());
                    if (cb_RS.Items.Count > 0)
                    {
                        cb_RS.SelectedIndex = 0;
                    }
                }
                else
                {
                }
            }
            catch
            {
            }
        }

        private void cb_RS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_RS.SelectedIndex < 0 || cb_RS.Items.Count <1 || tb_Name.Text.Trim().Length==0)
            {
                bt_Add.Enabled = false;
            }
            else
            {
                bt_Add.Enabled = true;
            }
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            if (cb_RS.SelectedIndex < 0 || cb_RS.Items.Count < 1 || tb_Name.Text.Trim().Length == 0)
            {
                bt_Add.Enabled = false;
            }
            else
            {
                bt_Add.Enabled = true;
            }
        }
    }
}